<template>
  <div class="department-container">
    <div class="app-container">
      <PageTools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>本月: 社保在缴 公积金在缴</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small" @click="uploadInput=true">导入excel</el-button>
          <el-button type="danger" size="small">导出excel</el-button>
          <el-button type="primary" size="small">新增员工</el-button>
        </template>
      </PageTools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" :index="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template slot-scope="scope">
              {{ farMat(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="pageParams.page"
            :total="total"
            :page-sizes="[1, 2, 3, 4,10]"
            :page-size="pageParams.size"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>

      </el-card>
    </div>
  </div>
</template>

<script>
import { getEmployees } from '@/api/employees'
import employees from '@/constant/employees'

const obj = employees.hireType.reduce((acc, cur) => {
  acc[cur.id] = cur.value
  return acc
}, {})
export default {
  name: 'Employees',
  data() {
    return {
      uploadInput: false,
      list: [],
      pageParams: {
        page: 1, // 查询第一页
        size: 10 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0

    }
  },
  computed: {
    index() {
      return (this.pageParams.page - 1) * this.pageParams.size + 1
    }
  },
  created() {
    this.loadEmployees()
  },
  methods: {
    farMat(typeNum) {
      console.log(typeNum)
      return obj[typeNum]
    },
    async loadEmployees() {
      const res = await getEmployees(this.pageParams)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.pageParams.size = val
      this.loadEmployees()
    },
    handleCurrentChange(page) {
      // 1. 更新页码
      this.pageParams.page = page
      // 2. 重发请求
      this.loadEmployees()
    }

  }
}
</script>

<style>

</style>
